package com.xoa.plugin.workflow.workflowservice.imlp.lnsfplugin;

import com.xoa.dev.efficiency.model.FlowAbilityMonitor;
import com.xoa.model.users.Users;
import com.xoa.model.workflow.FlowRun;
import com.xoa.model.workflow.TriggerPlugIn;
import com.xoa.plugin.workflow.workflowservice.WorkFlowService;
import com.xoa.util.ToJson;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Major_1 implements WorkFlowService {

    @Override
    public ToJson run(HttpServletRequest request) {
        String flowId = request.getParameter("flowId");
        String runId=request.getParameter("runId");
        Users users=LnsfUtil.init().getUser(request);
        Integer prcsId = 1;
        //获取办理时限
        String outTime = LnsfUtil.init().getWorkDeadline(flowId, prcsId);
        //获取转交的步骤，可能会有多个步骤并发情况1，2，3
        String nextWorks = request.getParameter("flowPrcs");
        //如果没有逗号，为了方便添加上逗号
        if (nextWorks != null && !nextWorks.contains(",")) {
            nextWorks = nextWorks + ",";
        }
        Map map=new HashMap();
        //用逗号分割
        String[] nextWorkArray = nextWorks.split(",");
        //按照php代码结构
        if (!nextWorks.contains("1,") && !nextWorks.contains("2,")) {
            FlowAbilityMonitor flowAbilityMonitor = new FlowAbilityMonitor();
            flowAbilityMonitor.setWorkTitle(LnsfUtil.init().getWorkNameValue(flowId,runId,"工作事项"));
            flowAbilityMonitor.setRunId(Integer.parseInt(runId));
            flowAbilityMonitor.setWorkDeadline(LnsfUtil.init().getWorkNameValue(flowId,runId,"办结期限"));//负责领导办结期限
            flowAbilityMonitor.setUserId(users.getUserId());
            flowAbilityMonitor.setDeptId(users.getDeptId() + "");
            flowAbilityMonitor.setWorkType(1);//任务类别：父流程
            flowAbilityMonitor.setUserType(1);//人员类别：负责校领导
            flowAbilityMonitor.setDeptType(1);//部门类别：负责校领导
            flowAbilityMonitor.setWorkStart(LnsfUtil.init().getSdf().format(new Date()));//负责领导开始时间
            LnsfUtil.init().setData(flowAbilityMonitor);
        } else if (nextWorkArray[0].equals("2")) {
            Map<String, Object> maps = new HashMap();
            maps.put("runId", runId);
            maps.put("prcsId", prcsId);
            maps.put("userId", users.getUserId());
            String prcsTime = LnsfUtil.init().selectOnePrcsTime(maps);
            if (prcsTime != null) {
                FlowAbilityMonitor flowAbilityMonitor = new FlowAbilityMonitor();
                flowAbilityMonitor.setRunId(Integer.parseInt(runId));
                flowAbilityMonitor.setWorkDeadline(LnsfUtil.init().getWorkNameValue(flowId,runId,"办结期限"));//负责领导办结期限
                flowAbilityMonitor.setUserId(users.getUserId());
                flowAbilityMonitor.setDeptId(users.getDeptId() + "");
                flowAbilityMonitor.setWorkType(1);//任务类别：父流程
                flowAbilityMonitor.setUserType(2);//人员类别：负责校领导
                flowAbilityMonitor.setDeptType(2);//部门类别：负责校领导
                flowAbilityMonitor.setWorkStart(LnsfUtil.init().getSdf().format(new Date()));//负责领导开始时间
                flowAbilityMonitor.setWorkTitle(LnsfUtil.init().getWorkNameValue(flowId,runId,"工作事项"));
                LnsfUtil.init().setData(flowAbilityMonitor);
            }
        } else if (nextWorkArray[0].equals("1")) {
            //有问题
            FlowAbilityMonitor flowAbilityMonitor = new FlowAbilityMonitor();
            flowAbilityMonitor.setRunId(Integer.parseInt(runId));
            //flowAbilityMonitor.setWorkDeadline(dataMap.get("办理期限"));//负责领导办结期限
            flowAbilityMonitor.setUserId(users.getUserId());
            flowAbilityMonitor.setDeptId(users.getDeptId() + "");
            flowAbilityMonitor.setWorkType(1);//任务类别：父流程
            flowAbilityMonitor.setUserType(4);//人员类别：负责校领导
            flowAbilityMonitor.setDeptType(4);//部门类别：负责校领导
            flowAbilityMonitor.setWorkStart(LnsfUtil.init().getSdf().format(new Date()));//负责领导开始时间
            LnsfUtil.init().setData(flowAbilityMonitor);

        }
        //结束第一步办理
        FlowAbilityMonitor flowAbilityMonitor = new FlowAbilityMonitor();
        flowAbilityMonitor.setRunId(Integer.parseInt(runId));
        flowAbilityMonitor.setUserId(users.getUserId());
        flowAbilityMonitor.setWorkType(1);
        flowAbilityMonitor.setUserType(2);
        flowAbilityMonitor.setWorkEnd(LnsfUtil.init().getSdf().format(new Date()));
        LnsfUtil.init().setData(flowAbilityMonitor);
        //获取重点子流程flowId
        Integer sonFlowId = LnsfUtil.init().getFlowParaMapper().getSonFlowId();
        //办公室督办意见
        List<String[]> dubanyijianList = LnsfUtil.init().getdubanyijian(LnsfUtil.init().getWorkNameValue(flowId,runId,"办公室督办意见"));
        //所建子流程
        List<FlowRun> flowRunSonList = LnsfUtil.init().getFlowAbilityMonitorMapper().getSonFlowRun(runId, sonFlowId.toString());
        try {
            String workDeadline=LnsfUtil.init().getWorkNameValue(flowId,runId,"办结期限");
            for (int i = 0; i < flowRunSonList.size(); i++) {
                FlowRun flowRunSon = flowRunSonList.get(i);
                if (flowRunSon != null) {
                    Integer runIdSon = flowRunSon.getRunId();
                    String beginUserSon = flowRunSon.getBeginUser();
                    Integer beginDeptSon = flowRunSon.getBeginDept();
                    String userNameSon =LnsfUtil.init().getUsersMapper().getUsernameByUserId(beginUserSon);
                    FlowAbilityMonitor abilityMonitor = new FlowAbilityMonitor();
                    abilityMonitor.setRunId(runIdSon);
                    abilityMonitor.setUserId(beginUserSon);
                    abilityMonitor.setDeptId(beginDeptSon + "");
                    abilityMonitor.setRunIdF(Integer.parseInt(runId));
                    abilityMonitor.setWorkType(2);
                    abilityMonitor.setUserType(3);
                    abilityMonitor.setDeptType(3);
                    abilityMonitor.setWorkStart(LnsfUtil.init().getSdf().format(new Date()));//负责领导开始时间
                    abilityMonitor.setRunTitleF(LnsfUtil.init().getWorkNameValue(flowId,runId,"工作事项"));
                    //如果督办意见不为空 null
                    try {
                        if (dubanyijianList.get(i) != null && !dubanyijianList.get(i).equals("")) {
                            if (userNameSon.equals(dubanyijianList.get(i)[2])) {
                                abilityMonitor.setWorkTitle(dubanyijianList.get(i)[0]);
                                abilityMonitor.setWorkDeadline(dubanyijianList.get(i)[3]);//负责领导办结期限
                                String sonTitle="";
                                if(abilityMonitor.getRunTitleF()!=null){
                                    sonTitle=abilityMonitor.getRunTitleF();
                                }
                                //更新子流程表单
                                String tableName = "flow_data_" + sonFlowId;
                                String setSql = "RUN_NAME='" + dubanyijianList.get(i)[0] + "',DATA_2='" + dubanyijianList.get(i)[0] +"',DATA_1='"+sonTitle+
                                        "',DATA_3='" + dubanyijianList.get(i)[3] + "',DATA_8='" + dubanyijianList.get(i)[1] + "',DATA_9='" +
                                        dubanyijianList.get(i)[2] + "'";
                                //修改子流程flowdata表
                                LnsfUtil.init().getWorkMapper().updateDataValue(setSql, tableName, runIdSon);
                                //修改流程名字
                                LnsfUtil.init().getFlowAbilityMonitorMapper().updateFlowRunNameByRunId(dubanyijianList.get(i)[0],runIdSon);
                            }
                        }
                    } catch (Exception e) {
                        abilityMonitor.setWorkDeadline(workDeadline);
                        abilityMonitor.setWorkTitle(flowRunSon.getRunName());
                    }
                    LnsfUtil.init().setData(abilityMonitor);
                }
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        try {
            String runName=LnsfUtil.init().getWorkNameValue(flowId,runId,"工作事项");
            if(runName!=null&&!runName.equals("")){
                LnsfUtil.init().getFlowAbilityMonitorMapper().updateFlowRunNameByRunId(LnsfUtil.init().getWorkNameValue(flowId,runId,"工作事项"),Integer.parseInt(runId));
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return null;
    }
}
